package com.thingsgrid.basicInterface.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.thingsgrid.basicInterface.vo.BasicInterfaceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thingsgrid.basicInterface.entity.BasicInterface;
import com.thingsgrid.basicInterface.service.IBasicInterfaceService;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2019-08-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/test/basicinterface")
@Api(value = "基础接口微服务", tags = "接口")
public class BasicInterfaceController extends BladeController {

	private IBasicInterfaceService basicInterfaceService;

	/**
	* 详情
	*/
	@GetMapping("/detail")
    @ApiOperationSupport(order = 1)
	@ApiOperation(value = "接口详情", notes = "传入basicInterface")
	public R<BasicInterface> detail(BasicInterface basicInterface) {
		BasicInterface detail = basicInterfaceService.getOne(Condition.getQueryWrapper(basicInterface));
		return R.data(detail);
	}


	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "接口分页", notes = "传入basicInterface")
	public R<IPage<BasicInterfaceVo>> list(@ApiIgnore @RequestParam Map<String, Object> basicInterfaceVo, Query query) {
		QueryWrapper<BasicInterfaceVo> queryWrapper = Condition.getQueryWrapper(basicInterfaceVo, BasicInterfaceVo.class);
		IPage<BasicInterfaceVo> pages = basicInterfaceService.selectApiandTypeName(Condition.getPage(query),queryWrapper);
		return R.data(pages);
	}



	/**
	* 新增或修改 
	*/
	@PostMapping("/submit")
    @ApiOperationSupport(order = 3)
	@ApiOperation(value = "接口新增或修改", notes = "传入basicInterface")
	public R submit(@Validated @RequestBody BasicInterface basicInterface) {

		if(null == basicInterface.getApiId()){
			BasicInterface one = basicInterfaceService.getOne(new LambdaQueryWrapper<BasicInterface>()
				.eq(BasicInterface::getApiName, basicInterface.getApiName())
				.eq(BasicInterface::getTypeId,basicInterface.getTypeId()));
			if(one != null){
				throw  new ApiException("接口类型已存在");
			}

		}
		return R.status(basicInterfaceService.saveOrUpdate(basicInterface));
	}

	
	/**
	* 删除 
	*/
	@PostMapping("/remove")
    @ApiOperationSupport(order = 4)
	@ApiOperation(value = "接口删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(basicInterfaceService.removeByIds(Func.toLongList(ids)));
	}



}
